<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useUserProfileStore } from '@/store'
import { ElMessage } from 'element-plus'
// import { ArrowDown, Apple, Android, Windows, Monitor, Mobile } from '@element-plus/icons-vue'

const router = useRouter()
const userProfileStore = useUserProfileStore()

// 控制滚动动画
const animationTriggers = ref([])
const headerRef = ref(null)
const devicesSectionRef = ref(null)
const featuresSectionRef = ref(null)
const downloadSectionRef = ref(null)
const devices = ref([])
const features = ref([])

// 滚动事件处理
let lastScrollTop = 0
const handleScroll = () => {
  const st = window.pageYOffset || document.documentElement.scrollTop
  const scrollDown = st > lastScrollTop
  const scrollPercent = st / (document.body.scrollHeight - window.innerHeight)
  
  // 视差效果 - 标题区域
  if (headerRef.value) {
    const translateY = st * 0.4
    const opacity = 1 - Math.min(1, st / 500)
    headerRef.value.style.transform = `translateY(${translateY}px)`
    headerRef.value.style.opacity = opacity
  }
  
  // 设备图片浮动效果
  if (devices.value.length) {
    devices.value.forEach((device, index) => {
      const speed = 0.05 + (index * 0.02)
      const yOffset = st * speed
      const rotation = (scrollDown ? 1 : -1) * Math.min(5, st * 0.01)
      device.style.transform = `translateY(${-yOffset}px) rotate(${rotation}deg)`
    })
  }
  
  // 平行移动效果 - 功能区域
  if (features.value.length) {
    features.value.forEach((feature, index) => {
      const delay = index * 100
      const direction = index % 2 === 0 ? -1 : 1
      const translateX = Math.min(0, -100 + (st - delay) * 0.2) * direction
      feature.style.transform = `translateX(${translateX}px)`
      feature.style.opacity = Math.min(1, (st - delay) * 0.003)
    })
  }
  
  // 下载按钮缩放效果
  if (downloadSectionRef.value) {
    const scale = 1 + Math.min(0.1, scrollPercent * 0.3)
    downloadSectionRef.value.style.transform = `scale(${scale})`
  }
  
  lastScrollTop = st <= 0 ? 0 : st
}

// 使用Intersection Observer检测元素进入视口
const setupIntersectionObserver = () => {
  const options = {
    root: null,
    rootMargin: '0px',
    threshold: 0.1
  }
  
  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.classList.add('in-view')
      }
    })
  }, options)
  
  // 观察所有带animate类的元素
  document.querySelectorAll('.animate').forEach(el => {
    observer.observe(el)
  })
  
  return observer
}

onMounted(() => {
  // 获取DOM元素引用
  devices.value = Array.from(document.querySelectorAll('.device-item'))
  features.value = Array.from(document.querySelectorAll('.feature-card'))
  
  // 设置滚动事件监听器
  window.addEventListener('scroll', handleScroll)
  
  // 设置Intersection Observer
  const observer = setupIntersectionObserver()
  animationTriggers.value.push(observer)
  
  // 初始化页面动画
  setTimeout(() => {
    document.querySelectorAll('.initial-animate').forEach(el => {
      el.classList.add('in-view')
    })
  }, 100)
})

onUnmounted(() => {
  // 移除滚动事件监听器
  window.removeEventListener('scroll', handleScroll)
  
  // 断开所有observers
  animationTriggers.value.forEach(observer => {
    if (observer && observer.disconnect) {
      observer.disconnect()
    }
  })
})

// 当用户信息突然没有时，重新登录
watch(userProfileStore.userInfo, () => {
  if (userProfileStore.userInfo.value === null) {
    ElMessage.error('用户信息获取失败，请重新登录')
    router.push('/login')
  }
})

</script>

<template>
  <div class="download-page">
    <!-- 顶部标题区域 -->
    <section class="header-section" ref="headerRef">
      <div class="header-content initial-animate fade-in">
        <h1 class="main-title">随时随地 畅听我们的歌</h1>
        <p class="subtitle">下载客户端，在您的手机、平板、电脑、电视、汽车、手表、音箱上播放海量高品质音乐和播客。</p>
        <div class="scroll-indicator">
          <el-icon class="bounce"><arrow-down /></el-icon>
          <span>向下滚动探索更多</span>
        </div>
      </div>
    </section>
    
    <!-- 设备展示区域 -->
    <section class="devices-section" ref="devicesSectionRef">
      <div class="section-title animate slide-up">
        <h2>多设备体验，无缝同步</h2>
        <p>一次登录，随处聆听，所有设备数据自动同步</p>
      </div>
      
      <div class="devices-display">
        <div class="device-item phone animate slide-right">
          <img src="../../assets/Phone.png" alt="手机应用界面">
          <div class="device-label">
            <el-icon><mobile /></el-icon>
            <span>手机</span>
          </div>
        </div>
        
        <div class="device-item tablet animate slide-up delay-100">
          <img src="../../assets/Pad.jpg" alt="平板应用界面">
          <div class="device-label">
            <el-icon><mobile /></el-icon>
            <span>平板</span>
          </div>
        </div>
        
        <div class="device-item desktop animate slide-left delay-200">
          <img src="../../assets/PC.jpg" alt="桌面应用界面">
          <div class="device-label">
            <el-icon><monitor /></el-icon>
            <span>电脑</span>
          </div>
        </div>
      </div>
    </section>
    
    <!-- 功能特点区域 -->
    <section class="features-section" ref="featuresSectionRef">
      <div class="section-title animate slide-up">
        <h2>专业功能，音乐发烧友的选择</h2>
        <p>无损音质播放，定制化推荐，海量音乐资源</p>
      </div>
      
      <div class="features-grid">
        <div class="feature-card animate fade-in delay-100">
          <div class="feature-icon sound-wave"></div>
          <h3>Hi-Res 无损音质</h3>
          <p>支持最高 24bit/192kHz 采样率，感受原汁原味的音乐魅力</p>
        </div>
        
        <div class="feature-card animate fade-in delay-200">
          <div class="feature-icon equalizer"></div>
          <h3>专业级音效调节</h3>
          <p>10段均衡器，多种预设音效，满足不同场景和设备需求</p>
        </div>
        
        <div class="feature-card animate fade-in delay-300">
          <div class="feature-icon download"></div>
          <h3>离线缓存</h3>
          <p>支持歌曲、专辑、播放列表一键下载，离线也能畅享音乐</p>
        </div>
        
        <div class="feature-card animate fade-in delay-400">
          <div class="feature-icon lyrics"></div>
          <h3>实时歌词</h3>
          <p>支持逐字歌词显示，卡拉OK模式，沉浸式歌词墙</p>
        </div>
      </div>
    </section>
    
    <!-- 下载按钮区域 -->
    <section class="download-section" ref="downloadSectionRef">
      <div class="download-container animate scale-in">
        <h2>立即体验</h2>
        <p>选择您的设备，开始音乐之旅</p>
        
        <div class="download-buttons">
          <a href="#" class="download-btn animate slide-up delay-100">
            <el-icon><apple /></el-icon>
            <div class="btn-text">
              <span class="small-text">下载</span>
              <span class="large-text">iOS 版</span>
            </div>
          </a>
          
          <a href="#" class="download-btn animate slide-up delay-200">
            <el-icon><android /></el-icon>
            <div class="btn-text">
              <span class="small-text">下载</span>
              <span class="large-text">Android 版</span>
            </div>
          </a>
          
          <a href="#" class="download-btn animate slide-up delay-300">
            <el-icon><windows /></el-icon>
            <div class="btn-text">
              <span class="small-text">下载</span>
              <span class="large-text">Windows 版</span>
            </div>
          </a>
        </div>
        
        <div class="platform-note animate fade-in delay-400">
          <p>同时支持 macOS、Linux、网页版以及各种智能设备</p>
        </div>
      </div>
    </section>
    
    <!-- 底部信息区域 -->
    <section class="info-section animate fade-in">
      <div class="info-grid">
        <div class="info-item">
          <h3>系统要求</h3>
          <ul>
            <li>iOS 12.0 或更高版本</li>
            <li>Android 6.0 或更高版本</li>
            <li>Windows 7 或更高版本</li>
            <li>macOS 10.12 或更高版本</li>
          </ul>
        </div>
        
        <div class="info-item">
          <h3>支持与帮助</h3>
          <ul>
            <li>安装指南</li>
            <li>常见问题</li>
            <li>技术支持</li>
            <li>反馈建议</li>
          </ul>
        </div>
        
        <div class="info-item">
          <h3>安全与隐私</h3>
          <p>所有应用均由官方开发，绿色无广告，保障您的隐私安全。</p>
        </div>
      </div>
    </section>
  </div>
</template>

<style scoped>
.download-page {
  overflow-x: hidden;
  background: linear-gradient(135deg, #1e1e1e 0%, #2d0a0a 50%, #1e1e1e 100%);
  color: #fff;
}

/* 顶部标题区域 */
.header-section {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.header-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(30, 0, 0, 0.7), rgba(120, 0, 0, 0.7));
  z-index: 1;
}

.header-content {
  max-width: 1000px;
  text-align: center;
  padding: 0 20px;
  position: relative;
  z-index: 2;
}

.main-title {
  font-size: 3rem;
  font-weight: bold;
  margin-bottom: 20px;
  line-height: 1.2;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

.subtitle {
  font-size: 1.2rem;
  line-height: 1.6;
  max-width: 800px;
  margin: 0 auto 50px;
}

.scroll-indicator {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 50px;
  opacity: 0.8;
  font-size: 0.9rem;
}

.bounce {
  animation: bounce 2s infinite;
  font-size: 24px;
  margin-bottom: 10px;
}

/* 设备展示区域 */
.devices-section {
  padding: 100px 20px;
  background: linear-gradient(to bottom, #20000d, #38050d);
  position: relative;
}

.devices-section::before {
  content: '';
  position: absolute;
  top: -100px;
  left: 0;
  width: 100%;
  height: 100px;
  background: linear-gradient(to bottom, transparent, #20000d);
}

.section-title {
  text-align: center;
  margin-bottom: 60px;
}

.section-title h2 {
  font-size: 2.5rem;
  margin-bottom: 15px;
  font-weight: bold;
  background: linear-gradient(90deg, #ff8a00, #e52e71);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.section-title p {
  font-size: 1.1rem;
  color: rgba(255, 255, 255, 0.8);
  max-width: 800px;
  margin: 0 auto;
}

.devices-display {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  max-width: 1200px;
  margin: 0 auto;
  gap: 40px;
  perspective: 1000px;
}

.device-item {
  position: relative;
  transition: transform 0.5s ease;
  transform-style: preserve-3d;
  cursor: pointer;
}

.device-item:hover {
  transform: translateY(-10px) scale(1.02);
  z-index: 10;
}

.device-item img {
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  transition: box-shadow 0.3s ease;
}

.device-item:hover img {
  box-shadow: 0 15px 40px rgba(255, 0, 0, 0.3);
}

.device-label {
  position: absolute;
  bottom: -15px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(90deg, #ff8a00, #e52e71);
  color: white;
  padding: 5px 15px;
  border-radius: 20px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 5px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.phone img {
  max-height: 400px;
}

.tablet img {
  max-height: 350px;
}

.desktop img {
  max-height: 300px;
}

/* 功能特点区域 */
.features-section {
  padding: 100px 20px;
  background: linear-gradient(to bottom, #38050d, #240017);
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
  max-width: 1200px;
  margin: 0 auto;
}

.feature-card {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  padding: 30px;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  text-align: center;
}

.feature-card:hover {
  transform: translateY(-10px);
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 10px 30px rgba(255, 0, 0, 0.2);
}

.feature-icon {
  width: 70px;
  height: 70px;
  margin: 0 auto 20px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  position: relative;
  overflow: hidden;
}

.feature-icon::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, #ff8a00, #e52e71);
  opacity: 0.8;
}

/* 各种功能图标的动画效果 */
.sound-wave::after {
  content: '';
  position: absolute;
  width: 60%;
  height: 50%;
  background-image: linear-gradient(to right, 
    transparent 0%, transparent 20%, 
    white 20%, white 22%, 
    transparent 22%, transparent 40%, 
    white 40%, white 42%, 
    transparent 42%, transparent 60%, 
    white 60%, white 62%, 
    transparent 62%, transparent 80%, 
    white 80%, white 82%, 
    transparent 82%, transparent 100%
  );
  animation: wave 2s infinite linear;
}

.equalizer::after {
  content: '';
  position: absolute;
  width: 60%;
  height: 50%;
  bottom: 15px;
  background-image: linear-gradient(to right, 
    white 0%, white 10%, 
    transparent 10%, transparent 15%, 
    white 15%, white 25%, 
    transparent 25%, transparent 30%, 
    white 30%, white 40%, 
    transparent 40%, transparent 45%, 
    white 45%, white 55%,
    transparent 55%, transparent 60%,
    white 60%, white 70%,
    transparent 70%, transparent 75%,
    white 75%, white 85%,
    transparent 85%, transparent 90%,
    white 90%, white 100%
  );
  animation: equalize 3s infinite alternate;
}

.download::after {
  content: '↓';
  color: white;
  font-size: 32px;
  animation: pulse 2s infinite;
}

.lyrics::after {
  content: '';
  position: absolute;
  width: 60%;
  height: 50%;
  background-image: linear-gradient(to right, 
    white 0%, white 60%, 
    transparent 60%, transparent 100%
  );
  animation: typing 3s infinite steps(6);
}

.feature-card h3 {
  font-size: 1.3rem;
  margin-bottom: 15px;
  font-weight: bold;
  color: #fff;
}

.feature-card p {
  font-size: 0.95rem;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1.6;
}

/* 下载按钮区域 */
.download-section {
  padding: 100px 20px;
  background: linear-gradient(to bottom, #240017, #0d0d0d);
  position: relative;
}

.download-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.1;
  z-index: 0;
}

.download-container {
  max-width: 1000px;
  margin: 0 auto;
  text-align: center;
  position: relative;
  z-index: 1;
}

.download-container h2 {
  font-size: 2.5rem;
  margin-bottom: 15px;
  font-weight: bold;
}

.download-container p {
  font-size: 1.1rem;
  margin-bottom: 40px;
  color: rgba(255, 255, 255, 0.8);
}

.download-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 30px;
}

.download-btn {
  display: flex;
  align-items: center;
  background: linear-gradient(45deg, #ff8a00, #e52e71);
  color: white;
  padding: 15px 25px;
  border-radius: 10px;
  text-decoration: none;
  transition: all 0.3s ease;
  min-width: 200px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
}

.download-btn:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(229, 46, 113, 0.4);
}

.download-btn .el-icon {
  font-size: 24px;
  margin-right: 15px;
}

.btn-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.small-text {
  font-size: 0.8rem;
  opacity: 0.9;
}

.large-text {
  font-size: 1.2rem;
  font-weight: bold;
}

.platform-note {
  margin-top: 30px;
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.6);
}

/* 底部信息区域 */
.info-section {
  padding: 80px 20px;
  background-color: #0d0d0d;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 40px;
  max-width: 1200px;
  margin: 0 auto;
}

.info-item h3 {
  font-size: 1.3rem;
  margin-bottom: 20px;
  color: #e52e71;
}

.info-item ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.info-item li {
  margin-bottom: 10px;
  color: rgba(255, 255, 255, 0.7);
  position: relative;
  padding-left: 20px;
}

.info-item li::before {
  content: '•';
  position: absolute;
  left: 0;
  color: #e52e71;
}

.info-item p {
  color: rgba(255, 255, 255, 0.7);
  line-height: 1.6;
}

/* 动画类 */
.animate {
  opacity: 0;
  transform: translateY(30px);
  transition: opacity 0.8s ease, transform 0.8s ease;
}

.animate.in-view {
  opacity: 1;
  transform: translateY(0);
}

.fade-in {
  opacity: 0;
  transition: opacity 1s ease;
}

.fade-in.in-view {
  opacity: 1;
}

.slide-up {
  transform: translateY(50px);
  opacity: 0;
  transition: transform 0.8s ease, opacity 0.8s ease;
}

.slide-up.in-view {
  transform: translateY(0);
  opacity: 1;
}

.slide-right {
  transform: translateX(-100px);
  opacity: 0;
  transition: transform 0.8s ease, opacity 0.8s ease;
}

.slide-right.in-view {
  transform: translateX(0);
  opacity: 1;
}

.slide-left {
  transform: translateX(100px);
  opacity: 0;
  transition: transform 0.8s ease, opacity 0.8s ease;
}

.slide-left.in-view {
  transform: translateX(0);
  opacity: 1;
}

.scale-in {
  transform: scale(0.8);
  opacity: 0;
  transition: transform 0.8s ease, opacity 0.8s ease;
}

.scale-in.in-view {
  transform: scale(1);
  opacity: 1;
}

/* 延迟类 */
.delay-100 {
  transition-delay: 0.1s;
}

.delay-200 {
  transition-delay: 0.2s;
}

.delay-300 {
  transition-delay: 0.3s;
}

.delay-400 {
  transition-delay: 0.4s;
}

/* 动画关键帧 */
@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-20px);
  }
  60% {
    transform: translateY(-10px);
  }
}

@keyframes wave {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 50px 0;
  }
}

@keyframes equalize {
  0% {
    height: 20%;
  }
  50% {
    height: 60%;
  }
  100% {
    height: 40%;
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes typing {
  0% {
    width: 0;
  }
  50% {
    width: 60%;
  }
  100% {
    width: 0;
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .main-title {
    font-size: 2.2rem;
  }
  
  .subtitle {
    font-size: 1rem;
  }
  
  .section-title h2 {
    font-size: 2rem;
  }
  
  .devices-display {
    flex-direction: column;
  }
  
  .device-item {
    width: 90%;
    max-width: 350px;
  }
  
  .phone img, .tablet img, .desktop img {
    max-height: unset;
    width: 100%;
  }
  
  .download-btn {
    width: 80%;
    justify-content: center;
  }
}
</style>